package com.wink.subscribe;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.storage.mongo.EComments;
import com.storage.mongo.ETopic;
import com.storage.mongo.Echarity;
import com.wink.charity.Charity;

public class SubscriptionList {

	private Morphia morp;
	private Mongo m; 
	private Datastore ds;
	
	
	public SubscriptionList() throws UnknownHostException, MongoException {

		morp = new Morphia();
		m = new Mongo( "localhost" , 27017 );
		//drop db
		//m.dropDatabase("mydb");
		
		morp.map(MongoSub.class);

		ds = morp.createDatastore("mydb");
	}
	
	public void addSub(Subscription sub) {
		MongoSub mSub = new MongoSub(sub.getEmail());
		
		if(sub.getAmount() != null) {
			mSub.setAmount(sub.getAmount());
		}
		
		if(sub.getName() != null) {
			mSub.setName(sub.getName());
		}
		
		if(sub.getCommunity() != null) {
			mSub.setCommunity(sub.getCommunity());
		}
		
		if(sub.getCommunityID() != null) {
			mSub.setCommunityID(sub.getCommunityID());
		}
		
		ds.save(mSub);
		System.out.println("Subscription created successfully");
	}
	
	public List<MongoSub> getAllSubs() {
		return ds.find(MongoSub.class).asList();
	}
	
	public List<Echarity> getCharitiesGivenSub(MongoSub sub) {
		Query<Echarity> q = ds.createQuery(Echarity.class).filter("charityName", "Charity1");
		return q.asList();
		
	}
}
